<template>
    <section>
        <!--<el-row>
            <el-col>
                <el-form :inline="true" :model="filter" class="demo-form-inline">
                    <el-form-item label="输入搜索">
                        <el-input v-model="filter.key" placeholder="邮箱/id"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="search">查询</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>-->
        <el-row>
            <el-col :span="24">
                <el-table :data="list" @selection-change="">
                    <el-table-column prop="avatar" label="通知发送者" width="120">
                        <template slot-scope="scope">
                            <img :src="scope.row.avatar" alt="头像" class="avatar">
                        </template>
                    </el-table-column>
                    <el-table-column prop="userName" label="通知发送者姓名" width="120">
                    </el-table-column>
                    <el-table-column prop="groupName" label="所属群组" width="120">
                    </el-table-column>
                    <el-table-column prop="created_at" label="发送时间" width="120">
                    </el-table-column>

                    <el-table-column prop="aaaa" label="操作" min-width="120">
                        <template slot-scope="scope">
                            <el-button @click="detail(scope.row)">通知详情</el-button>
                            <el-button @click="del(scope.row)">删除通知</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <div :span="24" style="text-align: center;margin-top: 20px">
            <el-pagination
                background
                layout="prev, pager, next"
                :total="total"
                :page-size="size"
                :current-page.sync="currentPage"
                @current-change="Init">
            </el-pagination>
        </div>

        <el-dialog
            title="评论内容"
            :visible.sync="dialogVisible"
            width="30%">
            <h1 style="text-align: center">{{current.title}}</h1>
            {{current.content}}
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">关闭</el-button>
            </span>
        </el-dialog>

    </section>
</template>
<script>
    /* eslint-disable indent,space-before-function-paren,spaced-comment */
    import {deleteNotice, getNoticeList} from '../../api/api'

    export default {
        data() {
            return {
                filter: {
                    key: ''
                },
                list: [],
                total: 0,
                size: 7,
                dialogVisible: false,
                current: {},
                currentPage: 0
            }
        },
        filters: {
            titleFilter(value) {
                if (value) {
                    return {
                        'phone': '电话',
                        'wallpaper': '背景',
                        'desc': '介绍',
                        'avatar': '头像',
                        'email': '邮箱',
                        'id': 'iD'
                    }[value]
                }
            }
        },
        methods: {
            Init(page = 1) {
                // debugger
                var para = {
                    page: page,
                    size: this.size
                }
                if (this.filter.key) {
                    para.key = this.filter.key
                }
                console.log(para.page)
                getNoticeList(para).then(
                    data => {
                        if (data.status) {
                            this.list = data.data.map(x => {
                                return {
                                    created_at: x.created_at,
                                    groupId: x.group_id,
                                    groupName: x.group_id,
                                    userName: x.user_info.name,
                                    avatar: x.user_info.avatar,
                                    title: x.title,
                                    content: x.content,
                                    id: x.id
                                }
                            })
                            if (page === 1) {
                                this.total = data.total
                                this.currentPage = 1
                            }
                        } else {
                            this.$message.error(data.msg)
                        }
                    }
                ).catch()
            },
            search() {
                this.Init()
            },
            detail(a) {
                this.current = a
                this.dialogVisible = true
            },
            del(b) {
                this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteNotice({
                        id: b.id
                    }).then(
                        data => {
                            if (data.status) {
                                this.$message.success('删除成功')
                                this.Init()
                            } else {
                                this.$message.error(data.msg)
                            }
                        }
                    ).catch()
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            }
        },
        mounted() {
            this.Init()
        }
    }
</script>
<style scoped>
    section {
        /*background: red;*/
    }

    .avatar {
        width: 50px;
        height: 50px;
        border-radius: 50%;
    }

    .wallpaper {
        width: 100%;
        border-radius: 8px;
    }
</style>
